package com.yndsoft.jc;

import android.app.Activity;
import android.app.Application;
import android.content.Context;

//import com.facebook.drawee.backends.pipeline.Fresco;
import com.squareup.leakcanary.LeakCanary;
import com.yndsoft.jc.common.AppException;

import java.util.LinkedList;
import java.util.List;

/**
 * Created by Administrator on 2016-08-01.
 */
//MyApplication类（储存每一个Activity，并实现关闭所有Activity的操作)
public class MyApplication extends Application {
    private List<Activity> activitys = null;
    private static MyApplication instance;

    public MyApplication() {
        //instance = this;
        activitys = new LinkedList<Activity>();
    }

    //单例模式中获取唯一的MyApplication实例
    public static synchronized MyApplication getInstance() {
        if (instance == null) {
            instance = new MyApplication();
        }
        return instance;
    }

    // 添加Activity到容器中
    public void addActivity(Activity activity) {
        if (activitys != null && activitys.size() > 0) {
            if (!activitys.contains(activity)) {
                activitys.add(activity);
            }
        } else {
            activitys.add(activity);
        }

    }

    // 遍历所有Activity并finish
    public void exit() {
        if (activitys != null && activitys.size() > 0) {
            for (Activity activity : activitys) {
                activity.finish();
            }
        }
        System.exit(0);
    }

    @Override
    public void onCreate() {
        super.onCreate();
        //初始化激光推送
        //JPushInterface.setDebugMode(true);
        //JPushInterface.init(this);
        //Fresco
//        Fresco.initialize(this);
        //内存泄露检测
        LeakCanary.install(this);
        //
        registerUncaughtExceptionHandler();
    }

    // 注册App异常崩溃处理器
    private void registerUncaughtExceptionHandler() {
        Thread.setDefaultUncaughtExceptionHandler(AppException.getAppExceptionHandler());
    }
    public static Context getContext() {
        return instance.getApplicationContext();
    }
}
